package com.zhijian.medical.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseHzMapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseMapper;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.RoutinePatientDiagnose;
import com.zhijian.medical.entity.po.RoutinePatientDiagnoseHz;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 会诊相关业务处理
 */
@Service
public class RoutinePatientDiagnoseHzService {

    @Autowired
    RoutinePatientDiagnoseHzMapper routinePatientDiagnoseHzMapper;

    @Autowired
    RoutinePatientDiagnoseMapper routinePatientDiagnoseMapper;

    public RoutinePatientDiagnoseHz addHz(RoutinePatientDiagnose.HzDockerInfo hzDockerInfo){
        QueryWrapper<RoutinePatientDiagnoseHz> hzQueryWrapper = new QueryWrapper<>();
        hzQueryWrapper.eq("diagnose_id", hzDockerInfo.getDiagnoseId());
        RoutinePatientDiagnoseHz old = routinePatientDiagnoseHzMapper.selectOne(hzQueryWrapper);
        if ( old == null){
            RoutinePatientDiagnoseHz routinePatientDiagnoseHz = new RoutinePatientDiagnoseHz();
            Date now = new Date();
            routinePatientDiagnoseHz.setDiagnoseId(hzDockerInfo.getDiagnoseId());
            routinePatientDiagnoseHz.setHzDocker(hzDockerInfo.getDockerId());
            routinePatientDiagnoseHz.setHzDockerName(hzDockerInfo.getDockerName());
            routinePatientDiagnoseHz.setCreateTime(now);
            routinePatientDiagnoseHz.setUpdateTime(now);
            routinePatientDiagnoseHzMapper.insert(routinePatientDiagnoseHz);
        }else {
            RoutinePatientDiagnoseHz upd = new RoutinePatientDiagnoseHz();
            Date now = new Date();
            upd.setDiagnoseId(hzDockerInfo.getDiagnoseId());
            upd.setHzDocker(hzDockerInfo.getDockerId());
            upd.setHzDockerName(hzDockerInfo.getDockerName());
            upd.setUpdateTime(now);
            upd.setId(old.getId());
            routinePatientDiagnoseHzMapper.updateById(upd);
        }
        // 添加出诊医生
        PersonDto user = CurrentUserHolder.getUser();
        RoutinePatientDiagnose routinePatientDiagnose = routinePatientDiagnoseMapper.selectById(hzDockerInfo.getDiagnoseId());
        if (Objects.nonNull(routinePatientDiagnose)) {
            routinePatientDiagnose.setFirstDockerName(user.getName());
            routinePatientDiagnose.setFirstDocker(user.getId());
            routinePatientDiagnoseMapper.updateById(routinePatientDiagnose);
        }
        return old;
    }

    public RoutinePatientDiagnoseHz readOne(Long patientId){
        QueryWrapper<RoutinePatientDiagnose> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", patientId);
        RoutinePatientDiagnose diagnose = routinePatientDiagnoseMapper.selectOne(queryWrapper);
        QueryWrapper<RoutinePatientDiagnoseHz> diagnoseQueryWrapper = new QueryWrapper<>();
        diagnoseQueryWrapper.eq("diagnose_id", diagnose.getId());
        return routinePatientDiagnoseHzMapper.selectOne(diagnoseQueryWrapper);
    }

    public RoutinePatientDiagnoseHz readOneByDiagnoseId(Long diagnoseId){
        QueryWrapper<RoutinePatientDiagnoseHz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("diagnose_id", diagnoseId);
        return routinePatientDiagnoseHzMapper.selectOne(queryWrapper);
    }

    public RoutinePatientDiagnoseHz updateHz(RoutinePatientDiagnoseHz diagnoseHz){
        Date now = new Date();
        QueryWrapper<RoutinePatientDiagnose> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", diagnoseHz.getPatientId());
        RoutinePatientDiagnose diagnose = routinePatientDiagnoseMapper.selectOne(queryWrapper);
        RoutinePatientDiagnoseHz old = readOneByDiagnoseId(diagnose.getId());
        if (old != null){
            diagnoseHz.setUpdateTime(now);
            diagnoseHz.setDiagnoseId(diagnose.getId());
            diagnoseHz.setId(old.getId());
            routinePatientDiagnoseHzMapper.updateById(diagnoseHz);
        }
        AssertUtil.check(old == null, "未发送会诊数据！");
        return diagnoseHz;
    }
}
